<template>
    <div>
        <el-dialog
                title="添加新课程"
                :visible.sync="saveNewCourseShow"
                :show-close="false"
                :before-close="handleClose">
            <el-form class="courseData" :model="CourseForm" :rules="rules" ref="ruleForm">
                <el-form-item label="课程名称" prop="name">
                    <el-input v-model.trim="CourseForm.name" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="课程价格" prop="price">
                    <el-input v-model.trim="CourseForm.price" minlength="1" maxlength="11" placeholder="请输入课程价格"/>
                </el-form-item>
                <el-form-item label="课时" prop="course_times">
                    <el-input-number v-model="CourseForm.course_times" @blur="handleChange" :min="1" :max="50"
                                     label="课时"></el-input-number>
                </el-form-item>
                <el-form label-position="right" :rules="rules" :model="CourseForm">
                    <el-form-item label="图片上传" prop="file">
                        <el-upload
                                :multiple="false"
                                ref="upload"
                                class="upload-demo"
                                action="#"
                                :auto-upload="false"
                                :file-list="HFfileList"
                                :before-upload="beforeUpload"
                                list-type="picture-card"
                                :on-preview="handlePictureCardPreview"
                                :on-remove="HFhandleRemove"
                                :on-change="HFhandleChangeImg">
                            <i class="el-icon-plus"></i>
                            <div slot="tip" class="el-upload__tip">
                                只能上传jpg/png文件，且不超过5Mb
                            </div>
                        </el-upload>
                    </el-form-item>
                </el-form>
            </el-form>
            <span slot="footer" class="dialog-footer">
         <el-button @click="cancel">取 消</el-button>
         <el-button type="primary" @click="submit" plain>确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {saveNewCourse} from '@/utils/apis/course'
    import upload from '@/utils/methods/upload'

    export default {
        name: "NewCourse",
        data() {
            let checkNumber = (rule, value, callback) => {
                if (value === "") {
                    callback(new Error("请输入正确的数字"));
                } else if (!this.isCellNumber(value)) {
                    callback(new Error("请输入正确的数字!"));
                } else {
                    callback();
                }
            };
            return {
                //上传图片url
                imgUrl: '',
                HFfileList: [],
                CourseForm: {
                    name: '',
                    price: '',
                    course_times: ''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入课程名称', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    price: [{required: true, validator: checkNumber, trigger: 'blur'}],
                    course_times: [{required: true, validator: checkNumber, trigger: 'blur'}]
                }
            }
        },
        props: ['saveNewCourseShow'],
        methods: {
            ...upload,
            //关闭弹窗
            handleClose() {
                this.cancel()
            },
            //当数字选择器中的数值发生变化
            handleChange() {
                if (this.CourseForm.course_times === undefined) {
                    this.CourseForm.course_times = 1
                }
            },
            cancel() {
                this.$emit('cancel')
                this.$notify.info('取消添加')
            },
            submit() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        const courseStr = JSON.stringify(this.CourseForm)
                        const formData = {
                            file: this.file,
                            courseStr: courseStr
                        }
                        console.log(formData)
                        saveNewCourse(formData).then(res => {
                            if (res.data.code === 200) {
                                this.$notify.success('添加成功')
                                this.$nextTick(() => {
                                    this.CourseForm = {
                                        name: '',
                                        price: '',
                                        course_times: ''
                                    }
                                    this.HFhandleRemove()
                                    this.HFfileList = []
                                    this.$emit('cancel')
                                    this.$emit('saveSuccess')
                                })
                            } else {
                                this.$notify.error(res.data.message)
                            }
                        })
                    } else {
                        this.$message.warning('请正确填写表单')
                    }
                })
            },
            //判断是否为纯数字
            isCellNumber(val) {
                if (!/^[0-9]*$/.test(val)) {
                    return false;
                } else {
                    return true;
                }
            },
        }
    }


</script>

<style scoped>
    .empData {
        background-color: rgba(246, 246, 246, 0.21);

    }
</style>